<?php
namespace app\SuperAdmin\controller;
use think\Db;
use think\Controller;
use think\Session;
class ExercisesController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('adminid'))
        {
            $this->redirect('index/index/index');
        }
    }

    public function index()
    {   
        $exercises=db("exercises")
        ->order('exerid asc')
        ->paginate(10);
        $this->assign('list',$exercises);
        return $this->fetch('exercises');
    }

     public function add(){
        $pic = request()->file('pic');
        if (!empty($pic)) 
        {
            $info = $pic->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static/img/exerimg');
            if ($info == false) {
                $this->$pic->getError();
            }
        $data=[
        'exername'=>input('name'),
        'exerintro'=>input('intro'),
        'exerstarttime'=>input('opentime'),
        'elevel'=>input('level'),
        'exeraddress'=>input('address'),
        'exerpic'=>$info->getSaveName(),
        ];
   
         if( Db::name("exercises")->insert($data)){
          $this->success('添加成功！');
        }
        else{
          $this->error('添加失败');
        }
        } 
        else 
        {
          $this->error('添加失败');
        }      
    }
    
    public function show(){
      $exerid=input('exerid');
      $list=Db::name('exercises')->where('exerid',$exerid)->find();
      echo(json_encode($list)) ;

    }

    public function delete(){
       $exerid=input('id');
       $exercises=Db::name("exercises")->find($exerid);
       $pic=$exercises["exerpic"];
       $file=ROOT_PATH."public".DS."static".DS. 'img'.DS.'exerimg'.DS.$pic;
       if (file_exists($file)) {
        unlink($file);
       }
       if (Db::table("exercises")->delete($exerid)){
         $this->success("删除成功");
       }
       else{
         $this->error("删除失败"); 
       }
    }

    public function edit(){
       $exerid=input('exerid');
       $file = request()->file('pic');
         $pic="";
        if($file){
            $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'img'.DS.'exerimg');
            if($info){
                $pic=$info->getFilename();
            }else{
                echo $file->getError();
            }
        $data=[
        'exername'=>input('name'),
        'exerintro'=>input('intro'),
        'exerstarttime'=>input('opentime'),
        'elevel'=>input('level'),
        'exeraddress'=>input('address'),
        'exerpic'=>$info->getSaveName(),
        ];
       if( Db::name('exercises')->where('exerid',$exerid)->update($data)){
          $this->success('更新成功！');
        }
        else{
          $this->error('更新失败');
        }
        }
        else{   
        $data=[
        'exername'=>input('name'),
        'exerintro'=>input('intro'),
        'exerstarttime'=>input('opentime'),
        'elevel'=>input('level'),
        'exeraddress'=>input('address'),
        ];
       if( Db::name('exercises')->where('exerid',$exerid)->update($data)){
          $this->success('更新成功！');
        }
        else{
          $this->error('更新失败');
        }
        }
    }

}